package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ActivityOrderDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 活动订单
 * */

@RestController
@RequestMapping("ActivityOrder")
public class ActivityOrderController extends BaseController{


    @Autowired
    @Qualifier("ActivityOrderServiceImplOne")
    private ActivityOrderService activityOrderService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("PracticeSiteServiceImplOne")
    private PracticeSiteService practiceSiteService;

    @Autowired
    @Qualifier("VolunteerTeamServiceImplOne")
    private VolunteerTeamService volunteerTeamService;


    @Autowired
    @Qualifier("CommandCenterServiceImplOne")
    private CommandCenterService commandCenterService;


    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("ActivityContentServiceImplOne")
    private ActivityContentService activityContentService;






    //实践站点点单
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String activityContentCode = jsonObject.getString("activityContentCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByUserCode(userCode);

            ActivityOrder activityOrder = new ActivityOrder();
            activityOrder.setActivityOrderCode(UUID.randomUUID().toString());
            activityOrder.setPracticeSiteCode(practiceSite.getPracticeSiteCode());
            activityOrder.setActivityContentCode(activityContentCode);
            activityOrder.setStatus(1);
            activityOrder.setCreateTime(new Date());
            activityOrder.setGovernmentCode(practiceSite.getGovernmentCode());
            activityOrder.setCommandCenterCode(practiceSite.getCommandCenterCode());
            activityOrderService.insert(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //指挥中心派单
    @RequestMapping(value = "Pass", method = RequestMethod.POST)
    public MyResult Pass(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String staffCode = jsonObject.getString("staffCode");
            String activityOrderCode = jsonObject.getString("activityOrderCode");
            String volunteerTeamCode = jsonObject.getString("volunteerTeamCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityOrder/Pass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);
            activityOrder.setVolunteerTeamCode(volunteerTeamCode);
            activityOrder.setPassTime(new Date());
            activityOrder.setStatus(2);
            activityOrderService.update(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Pass",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //志愿队伍接单
    @RequestMapping(value = "Receive", method = RequestMethod.POST)
    public MyResult Receive(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            //String userCode = jsonObject.getString("userCode");
            String activityOrderCode = jsonObject.getString("activityOrderCode");

            //String address = jsonObject.getString("address");
            //Date startTime = jsonObject.getDate("startTime");
            //Date endTime = jsonObject.getDate("endTime");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/Receive");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);
            //activityOrder.setActivityStartTime(startTime);
            //activityOrder.setActivityEndTime(endTime);
            activityOrder.setReceiveTime(new Date());
            //activityOrder.setActivityAddress(address);
            activityOrder.setStatus(3);
            activityOrderService.update(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Receive",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //志愿队伍活动开始
    @RequestMapping(value = "Start", method = RequestMethod.POST)
    public MyResult Start(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String activityOrderCode = jsonObject.getString("activityOrderCode");

            String address = jsonObject.getString("address");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/Start");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);
            activityOrder.setActivityStartTime(new Date());
            activityOrder.setStatus(4);
            activityOrder.setActivityAddress(address);
            activityOrderService.update(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Start",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //志愿队伍活动结束
    @RequestMapping(value = "End", method = RequestMethod.POST)
    public MyResult End(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String activityOrderCode = jsonObject.getString("activityOrderCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/End");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);
            activityOrder.setActivityEndTime(new Date());
            activityOrder.setStatus(5);
            activityOrderService.update(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/End",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //实践站点评价
    @RequestMapping(value = "Evaluate", method = RequestMethod.POST)
    public MyResult Evaluate(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String activityOrderCode = jsonObject.getString("activityOrderCode");
            String msg = jsonObject.getString("msg");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/Evaluate");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);
            activityOrder.setEvaluateTime(new Date());
            activityOrder.setEvaluate(msg);
            activityOrder.setStatus(6);
            activityOrderService.update(activityOrder);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Evaluate",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    /**
     * 已点单,已派单，活动中，已评价等的列表
     * 指挥中心
     * */
    @RequestMapping(value = "PageForCenter",method = RequestMethod.POST)
    public MyResult PageForCenter(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            int status = jsonObject.getInteger("status");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityOrder/PageForCenter");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            String commandCenterCode = "";
            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);
            if(commandCenterStaffRel != null){
                commandCenterCode = commandCenterStaffRel.getCommandCenterCode();
            }

            PageInfo<ActivityOrder> pageInfo = activityOrderService.PageByCommandCenterCode(pageIndex,pageSize,
                    staff.getGovernmentCode(),commandCenterCode,status);

            List<ActivityOrderDTO> listDTO = new ArrayList<>();
            for(ActivityOrder activityOrder:pageInfo.getList()){
                ActivityOrderDTO activityOrderDTO = mapper.map(activityOrder,ActivityOrderDTO.class);

                ActivityContent activityContent = activityContentService.getByActivityContentCode(activityOrder.getActivityContentCode());
                activityOrderDTO.setActivityContentName(activityContent.getName());

                VolunteerTeam volunteerTeam = volunteerTeamService.getByVolunteerTeamCode(activityOrder.getVolunteerTeamCode());
                if(volunteerTeam != null){
                    User user = userService.getByUserCode(volunteerTeam.getUserCode());
                    activityOrderDTO.setVolunteerTeamName(user.getName());
                }

                PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(activityOrder.getPracticeSiteCode());
                if(practiceSite != null){
                    User user = userService.getByUserCode(practiceSite.getUserCode());
                    activityOrderDTO.setPracticeSiteName(user.getName());
                }

                listDTO.add(activityOrderDTO);
            }

            PageInfo<ActivityOrderDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/PageForCenter", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 已点单,已派单，活动中，已评价等的列表
     * 实践站点
     * */
    @RequestMapping(value = "PageForSite",method = RequestMethod.POST)
    public MyResult PageApplyForSite(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            int status = jsonObject.getInteger("status");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/PageForSite");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByUserCode(userCode);

            PageInfo<ActivityOrder> pageInfo = activityOrderService.PageByPracticeSiteCode(pageIndex,pageSize,
                    practiceSite.getPracticeSiteCode(),status);

            List<ActivityOrderDTO> listDTO = new ArrayList<>();
            for(ActivityOrder activityOrder:pageInfo.getList()){
                ActivityOrderDTO activityOrderDTO = mapper.map(activityOrder,ActivityOrderDTO.class);

                ActivityContent activityContent = activityContentService.getByActivityContentCode(activityOrder.getActivityContentCode());
                activityOrderDTO.setActivityContentName(activityContent.getName());

                VolunteerTeam volunteerTeam = volunteerTeamService.getByVolunteerTeamCode(activityOrder.getVolunteerTeamCode());
                if(volunteerTeam != null){
                    User user = userService.getByUserCode(volunteerTeam.getUserCode());
                    activityOrderDTO.setVolunteerTeamName(user.getName());
                }

                listDTO.add(activityOrderDTO);
            }

            PageInfo<ActivityOrderDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/PageForSite", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 已派单，已接单，活动中，已评价等的列表
     * 志愿队伍
     * */
    @RequestMapping(value = "PageForVolunteerTeam",method = RequestMethod.POST)
    public MyResult PageForVolunteerTeam(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            int status = jsonObject.getInteger("status");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/PagePassForVolunteerTeam");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            VolunteerTeam volunteerTeam = volunteerTeamService.getByUserCode(userCode);

            PageInfo<ActivityOrder> pageInfo = activityOrderService.PageByVolunteerTeamCode(pageIndex,pageSize,
                    volunteerTeam.getVolunteerTeamCode(),status);

            List<ActivityOrderDTO> listDTO = new ArrayList<>();
            for(ActivityOrder activityOrder:pageInfo.getList()){
                ActivityOrderDTO activityOrderDTO = mapper.map(activityOrder,ActivityOrderDTO.class);

                ActivityContent activityContent = activityContentService.getByActivityContentCode(activityOrder.getActivityContentCode());
                activityOrderDTO.setActivityContentName(activityContent.getName());

                VolunteerTeam volunteerTeam1 = volunteerTeamService.getByVolunteerTeamCode(activityOrder.getVolunteerTeamCode());
                if(volunteerTeam1 != null){
                    User user = userService.getByUserCode(volunteerTeam1.getUserCode());
                    activityOrderDTO.setVolunteerTeamName(user.getName());
                }

                listDTO.add(activityOrderDTO);
            }

            PageInfo<ActivityOrderDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/PagePassForVolunteerTeam", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }





    /**
     * 已评价点单列表
     * 小程序
     * */
    @RequestMapping(value = "PageEvaluateForXCX",method = RequestMethod.POST)
    public MyResult PageEvaluateForXCX(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String commandCenterCode = jsonObject.getString("commandCenterCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/PageApplyForXCX");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenter commandCenter = commandCenterService.getByCommandCenterCode(commandCenterCode);
            String governmentCode = commandCenter.getGovernmentCode();

            PageInfo<ActivityOrder> pageInfo = activityOrderService.PageByCommandCenterCode(pageIndex,pageSize,
                    governmentCode,commandCenterCode,6);

            List<ActivityOrderDTO> listDTO = new ArrayList<>();
            for(ActivityOrder activityOrder:pageInfo.getList()){
                ActivityOrderDTO activityOrderDTO = mapper.map(activityOrder,ActivityOrderDTO.class);

                ActivityContent activityContent = activityContentService.getByActivityContentCode(activityOrder.getActivityContentCode());
                activityOrderDTO.setActivityContentName(activityContent.getName());

                VolunteerTeam volunteerTeam = volunteerTeamService.getByVolunteerTeamCode(activityOrder.getVolunteerTeamCode());
                if(volunteerTeam != null){
                    User user = userService.getByUserCode(volunteerTeam.getUserCode());
                    activityOrderDTO.setVolunteerTeamName(user.getName());
                }

                listDTO.add(activityOrderDTO);
            }

            PageInfo<ActivityOrderDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/PageApplyForXCX", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 点单详情
     * 小程序
     * */
    @RequestMapping(value = "Get",method = RequestMethod.POST)
    public MyResult Get(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String activityOrderCode = jsonObject.getString("activityOrderCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrder/Get");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityOrder activityOrder = activityOrderService.getByActivityOrderCode(activityOrderCode);

            ActivityOrderDTO activityOrderDTO = mapper.map(activityOrder,ActivityOrderDTO.class);

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(activityOrder.getPracticeSiteCode());
            User user = userService.getByUserCode(practiceSite.getUserCode());
            activityOrderDTO.setPracticeSiteName(user.getName());

            ActivityContent activityContent = activityContentService.getByActivityContentCode(activityOrder.getActivityContentCode());
            activityOrderDTO.setActivityContentName(activityContent.getName());

            VolunteerTeam volunteerTeam = volunteerTeamService.getByVolunteerTeamCode(activityOrder.getVolunteerTeamCode());
            user = userService.getByUserCode(volunteerTeam.getUserCode());
            activityOrderDTO.setVolunteerTeamName(user.getName());

            CommandCenter commandCenter = commandCenterService.getByCommandCenterCode(activityOrder.getCommandCenterCode());
            user = userService.getByUserCode(commandCenter.getUserCode());
            activityOrderDTO.setCommandCenterName(user.getName());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("activityOrderDTO",activityOrderDTO);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrder/Get", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
